<?php


namespace services\dengta\robot;

use common\components\Service;
use common\helpers\CommonHelper;
use Dompdf\Dompdf;
use Dompdf\Options;
use Yii;

class MessageService extends Service
{
    const MESSAGE_TEXT = 1; // 文本内容
    const MESSAGE_IMAGE = 2; // 图片消息
    const MESSAGE_VOICE = 3; // 语音消息
    const MESSAGE_VIDEO = 4; // 视频消息
    const MESSAGE_SYSTEM = 5; // 系统消息
    const MESSAGE_GZH = 6; // 公众号文章推送
    const MESSAGE_FILE = 8; // 文件发送
    const MESSAGE_LUCKY_MONEY = 11; // 红包
    const MESSAGE_MONEY_TRANS = 12; // 转账
    const MESSAGE_EMOJI = 14; // 表情
    const MESSAGE_REFUSE = 19; // 音视频通话拒绝提示
    const MESSAGE_OTHER = 20; // 其它

    public static $contentTypes = [
        self::MESSAGE_TEXT => '文本内容',
        self::MESSAGE_IMAGE => '图片消息',
        self::MESSAGE_VOICE => '语音消息',
        self::MESSAGE_VIDEO => '视频消息',
        self::MESSAGE_SYSTEM => '系统消息',
        self::MESSAGE_FILE => '文件发送',
        self::MESSAGE_LUCKY_MONEY => '红包',
        self::MESSAGE_MONEY_TRANS => '转账',
        self::MESSAGE_EMOJI => '表情',
    ];

    /**
     * 导出PDF格式聊天记录
     *
     * @param array $params
     * @return mixed
     */
    public function downloadToPdf(array $params)
    {
        $params['type'] = 'all';

//        $userService = new WechatService();
//        $data = $userService->getWechatMessageList($params, (int)0, (int)0);
        $data = CommonHelper::dengta_request('/v1/baiyou/wechat-message-list', array_merge($params, ['page' => 0, 'pageSize' => 0]));

        if (empty($data['list'])) {
            return CommonHelper::api(422, '暂无聊天内容');
        }

        $messageHtml = '';
        $startDate = $endDate = '';
        foreach ($data['list'] as $key => $row) {
            if ($key == 0) {
                $startDate = $endDate = date('Y-m-d', strtotime($row['create_time']));
            } else {
                $startDate = date('Y-m-d', strtotime($row['create_time']));
            }

            $isChange = 1;
            if ($key > 0 && $data['list'][$key - 1]['isSend'] == $row['isSend']) {
                $isChange = 0;
            }

            if ($isChange) {
                $messageHtml .= "
                    <!-- 头像与id时间 -->
                    <div style=\"position: relative;padding-top: 8px;\">
                        <div>
                            <img style=\"width:40px;height:40px\" src=\"{$row['avatar']}\" alt=\"\">
                        </div>
                        <div style=\"position: absolute;top: 0;left:48px;font-size: 14px;\">
                            <div style=\"padding-bottom: 4px;\">id: {$row['name']}</div>
                            <div>{$row['create_time']}</div>
                        </div>
                    </div>
                ";
            } else {
                $messageHtml .= "
                    <!-- 聊天时间 -->
                    <span style=\"font-size: 14px;\">{$row['create_time']}</span>
                ";
            }

            switch ((int)$row['contentType']) {
                case self::MESSAGE_TEXT:
                case self::MESSAGE_SYSTEM:
                    $messageHtml .= "
                        <!-- 聊天内容 -->
                        <div>
                            <!-- 文字 -->
                            <p style=\"width:100%;word-wrap: break-word;word-break: normal;\">{$row['content']}</p>
                        </div>
                    ";
                    break;
                case self::MESSAGE_IMAGE:
                    $url = $row['fileUrl'] ? $row['fileUrl'] : $row['content'];

                    $messageHtml .= "
                        <div>
                            <!-- 图片 -->
                            <img style=\"max-height:350px\" src=\"{$url}\">
                        </div>
                    ";
                    break;
                default:
                    $content = self::$contentTypes[$row['contentType']] ?? '未知消息';

                    $messageHtml .= "
                        <!-- 聊天内容 -->
                        <div>
                            <!-- 文字 -->
                            <p style=\"width:100%;word-wrap: break-word;word-break: normal;\">[{$content}]</p>
                        </div>
                    ";
                    break;
            }

            $messageHtml .= "
                <!-- 分割线 -->
                <div style=\"width:100%;border-bottom: 1px solid #eee;padding: 8px 0\"></div>
            ";
        }

        $html = "
            <!DOCTYPE html>
            <html lang=\"en\">
            <head>
                <meta charset=\"gb2312\">
                <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
                <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
                <title>Document</title>
            </head>
            <!-- 支持中文字体 -->
            <body style=\"font-family:Droid;width:100%;\">
                <!-- 标题 -->
                <h3 style=\"text-align: center;\">{$startDate}至{$endDate}</h3>
                <!-- 分割线 -->
                <div style=\"width:100%;border-bottom: 1px solid #eee;padding: 8px 0\"></div>
                <!-- 聊天内容 -->
                {$messageHtml}
            </body>
            </html>
        ";

        $options = new Options();
        $options->setIsRemoteEnabled(true);

        $dompdf = new Dompdf($options);
        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation
        $dompdf->setPaper('A4', 'portrait');

        // Render the HTML as PDF
        $dompdf->render();

        // Output the generated PDF to Browser
        $dompdf->stream();
    }
}
